package com.situ.ftbserver.api;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.situ.ftbserver.model.Member;
import com.situ.ftbserver.model.search.MemberSearchBean;
import com.situ.ftbserver.service.MemberService;
import com.situ.ftbserver.util.JsonResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDate;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping(value = "/api/v1/members",produces = MediaType.APPLICATION_JSON_VALUE)
public class MemberApi {
    private MemberService memberService;

    @Autowired
    public void setMemberService(MemberService memberService) {
        this.memberService = memberService;
    }

    @GetMapping
    public ResponseEntity<JsonResult<?>> findAll(@RequestParam(defaultValue ="1")  Integer pageNo,
                                                 @RequestParam(defaultValue ="10") Integer pageSize,
                                                 MemberSearchBean msb) {
        Page<Member> page = Page.of(pageNo, pageSize);

        page = this.memberService.findAll(page, msb);
//        System.out.println( page);
        return ResponseEntity.ok(JsonResult.success(page));
    }
    @GetMapping("/getMember")
    public ResponseEntity<JsonResult<?>> getMember(){
        List<Member> members = this.memberService.getMember();
        return ResponseEntity.ok(JsonResult.success(members));
    }
    @GetMapping("/getMemberAgeCount")
    public ResponseEntity<JsonResult<?>> getMemberAgeCount(){
        List<Map<String, Integer>> memberAgeCount = memberService.getMemberAgeCount();
        return ResponseEntity.ok(JsonResult.success(memberAgeCount));
    }
    @DeleteMapping
    public ResponseEntity<JsonResult<?>> delete(@RequestBody Integer[] ids){
//        System.out.println(ids);
        int delete = memberService.delete(ids);
        if(delete==0){
            return ResponseEntity.ok(JsonResult.fail("删除失败,所选会员有预约的课程"));
        }
        return ResponseEntity.ok(JsonResult.success(delete));
    }

    @PostMapping
    public ResponseEntity<JsonResult<?>> save(@RequestBody @Validated Member member){
        LocalDate birthday = member.getBirthday();
        member.setAge(LocalDate.now().getYear() - birthday.getYear());
//        System.out.println(member.getAge());
        boolean result = this.memberService.save(member);
        if(result){
            return ResponseEntity.ok(JsonResult.success(member));
        }else{
            return ResponseEntity.ok(JsonResult.fail("保存失败"));
        }
    }
    @PutMapping
    public ResponseEntity<JsonResult<?>> update(@RequestBody @Validated Member member){
        LocalDate birthday = member.getBirthday();
        member.setAge(LocalDate.now().getYear() - birthday.getYear());
        boolean result = this.memberService.updateById(member);
        if(result){
            return ResponseEntity.ok(JsonResult.success(member));
        }else{
            return ResponseEntity.ok(JsonResult.fail("更新失败"));
        }
    }
}
